<template>
    <div>
        <el-button type="success" @click="synthesizeSpeech">Web Speech API</el-button>
        <Voice :ref="'voice' + index" :id="index" url="https://educationview.oss-ap-northeast-1.aliyuncs.com/2024/12/28/audio/d4c02a5a-5b1b-4ab2-8f6d-b2d6c14d32cf.mp3" ></Voice>
    </div>

</template>
<script>
import Voice from '@/components/voice.vue'
import { post } from "@/http/axios"

export default {
    components: {
        Voice
    },
    data() {
        return {
            url: ""
        }
    },
    methods: {
        pauseAi() {

        },
        async synthesizeSpeech() {
            const audioData = await post("https://texttospeech.googleapis.com/v1/text:synthesize?key=AIzaSyD9svMNBcCUbDn86-QdHCtUrY2Hp1Nn2V8",
                {
                    input: {
                        text: "1.14"
                    },
                    voice: {
                        "languageCode": "ja-JP",
                        "name": "ja-JP-Wavenet-A",
                        "ssmlGender": "FEMALE",
                    },
                    audioConfig: {
                        "audioEncoding": "MP3",
                        "speakingRate": 1.0,
                        "pitch": 0,
                    }
                }
            )
            const audioContent = audioData.data.audioContent;
            const audioDatas = Uint8Array.from(atob(audioContent), c => c.charCodeAt(0)); // Base64 解码

            const formData = new FormData();
            const blob = new Blob([audioDatas.buffer], { type: 'audio/mp3' });
            console.log(blob)
            formData.append('file', blob, 'output.mp3');
            let qq = await post("/testAudo", formData)
            this.url = qq.data.url
        }
    }
}
</script>